package com.example.stlink.utils.httpUtils;

import android.os.NetworkOnMainThreadException;

import androidx.annotation.NonNull;

import com.example.stlink.model.constants.UrlConstants;
import com.example.stlink.model.response.BaseResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public abstract class DeleteUrlParamHttpUtils {

    /**
     * post请求
     * @param url 网络请求地址
     */
    public void deleteConnect(String url){
        System.out.println("GET: loading...........");

        //请求头
        Headers headers = new Headers.Builder()
                .add("appId", UrlConstants.APP_ID)
                .add("appSecret", UrlConstants.APP_SECRET)
                .build();

        //请求组合创建
        Request request = new Request.Builder()
                .url(url)
                .headers(headers)
                .delete()
                .build();
        try {
            OkHttpClient client = new OkHttpClient();
            //发起请求
            System.out.println("GET:URL:" + url);
            System.out.println("GET:CONNECT:start...........");
            client.newCall(request).enqueue(callback);
        }catch (NetworkOnMainThreadException ex){
            ex.printStackTrace();
        }
    }


    private final Callback callback = new Callback() {
        /**
         * 主要是连接异常错误
         * @param call 回调
         * @param e 异常
         */
        @Override
        public void onFailure(@NonNull Call call, IOException e) {
            //Log.e(TAG, "Failed to connect server!!!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(@NonNull Call call, Response response) throws IOException {
            String body = null;
            if(response.body() != null){
                body = response.body().string();
            }
            Gson gson = new Gson();
            Type jsonType = new TypeToken<BaseResponse<Object>>() {}.getType();
            BaseResponse<Object> baseResponse = gson.fromJson(body, jsonType);
            assert baseResponse != null;
            int code = baseResponse.getCode();
            //页面请求成功
            if(response.code() == 200){
                if (code == 200) {
                    System.out.println("GET:nextMethod...........");
                    System.out.println("GET:code = " + baseResponse.getCode() + "...................");
                    System.out.println("baseResponse:code = " + response.code());
                    nextMethod(baseResponse);
                }else if(code == 404){
                    System.out.println("资源找不到....................");
                }else if(code == 500){
                    System.out.println("GET:failed.............");
                    System.out.println("baseResponse:msg = " + baseResponse.getMsg() + ".........................");
                    System.out.println("GET:failedMethod");
                    failedMethod(baseResponse);
                }else if(code == 5217){
                    System.out.println("没有或缺少 appId 或 appSecret...........................");
                }else if(code == 5311){
                    System.out.println("接口使用次数不够................");
                }else if(code == 5314){
                    System.out.println("请求参数不对.................");
                }else{
                    System.out.println("GET:connect...error...................");
                    System.out.println("GET:code = " + response.code());
                }
            }else if(code == 401){
                System.out.println("未授权.........................");
            }else{
                System.out.println("GET:connect...error...................");
                System.out.println("GET:code = " + response.code());
                System.out.println("response: " + response);
                System.out.println("baseResponse: " + baseResponse);
            }

        }
    };


    /**
     * post成功的后续动作
     */
    public abstract void nextMethod(BaseResponse<Object> baseResponse);

    /**
     * post失败的后续动作
     */
    public abstract void failedMethod(BaseResponse<Object> baseResponse);

}
